package newcrunch.util;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.text.JTextComponent;
import javax.swing.text.Segment;
import jsyntaxpane.Token;
import newcrunch.lexers.JavaCommentsLexer;
import newcrunch.typography.TokenPosWithLine;
import newcrunch.typography.TokenPosition;

/**
 * @author WL
 */
public class LexicalUtility 
{
    protected LexicalUtility() {}
    
    /**
     * Finds the positions in the code which is longer
     * than the given maximum length.
     * 
     * TokenPosition contains the line and the length.
     * @param text
     * @param maximum
     * @return 
     */
    public static List<TokenPosition> getLineLengths(String text)
    {
        List<TokenPosition> result = new ArrayList<TokenPosition>();
        
        BufferedReader reader = new BufferedReader(new StringReader(text));
        
        int line = 1;
        String curr = null;
        try {
            while ((curr = reader.readLine()) != null)
            {
                result.add(new TokenPosition(line, curr.length()));
                    
                line++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    /**
     * Gets the position of all blank lines within the text.
     * info should be an integer array of 2 elements.
     * The first element will receive the number of blank lines,
     * whereas the second will receive the total number of lines.
     * @param text
     * @param info
     * @return 
     */
    public static List<TokenPosWithLine> getBlankLines(String text, int[] info)
    {
        BufferedReader reader = new BufferedReader(new StringReader(text));
        List<TokenPosWithLine> result = new ArrayList<TokenPosWithLine>();
        
        int pos = 0;
        int line = 0;
        int blanks = 0;
        String curr = null;
        try {
            while ((curr = reader.readLine()) != null)
            {
                if (curr.trim().isEmpty())
                {
                    result.add(new TokenPosWithLine(pos, pos + curr.length() - 1, line + 1, line + 1));
                    blanks++;
                }
                
                pos += curr.length() + 1;
                line++;
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        if (info != null && info.length >= 2)
        {
            info[0] = blanks;
            info[1] = line;
        }
        
        return result;
    }
    
    public static List<TokenPosWithLine> getComments(JTextComponent editor)
    {
        int len = editor.getDocument().getLength();
        List<Token> toks = new ArrayList<Token>(len / 10);
        Segment seg = new Segment();
        try {
            editor.getDocument().getText(0, len, seg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        JavaCommentsLexer lexer = new JavaCommentsLexer();
	lexer.parse(seg, 0, toks);
        
        return lexer.getComments();
    }
}
